package com.example.victory.service.fsdp;


import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.example.victory.config.ServiceError;
import com.example.victory.entity.companyEntity.FsdpResponseEntity;
import com.example.victory.utils.CrmUtils;
import com.cgws.fintech.sdk.api.inner.security.model.User;
import com.cgws.fintech.sdk.common.exception.GlobalException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;

@Service
@Slf4j
public class FsdpUtilService {

    @Autowired
    private FsdpService fsdpService;

    /**
     * 统一请求fsdp方法
     * @param param  请求入参
     * @return  fsdp响应
     */
    public  String requestFsdp(JSONObject param, String serviceId) {
        param.put("serviceId", serviceId);
        String fsdpParam = param.toJSONString();
        log.info("FSDP请求参数：{}", fsdpParam);
        ResponseEntity<String>  stringResponseEntity;
        try {
            stringResponseEntity = fsdpService.getFsdpInfo(fsdpParam);
        } catch (Exception ex) {
            log.error("请求fsdp异常,e={}", ex);
            throw new GlobalException(ServiceError.REQUEST_FSDP_ERROR);
        }
        if(stringResponseEntity.getStatusCode()!= HttpStatus.OK||StringUtils.isEmpty(stringResponseEntity.getBody()))
            throw new GlobalException(ServiceError.REQUEST_RESPONSE_ERROR);
        FsdpResponseEntity fsdpResponseEntity=null;
        try {
             fsdpResponseEntity= JSON.parseObject(URLDecoder.decode(stringResponseEntity.getBody(),"UTF-8"), FsdpResponseEntity.class);
            log.info(String.format("响应%s为%s",param.getString("serviceId"),JSON.toJSONString(fsdpResponseEntity)));
            if(fsdpResponseEntity==null){
                log.error("解析FSDP响应体为null");
                throw new GlobalException(ServiceError.REQUEST_RESPONSE_ERROR);
            }
        } catch (UnsupportedEncodingException e) {
            log.error(String.format("解密FSDP或者转换类型错误,response=%s,e=%s",stringResponseEntity.getBody(),e));
            throw new GlobalException(ServiceError.RESPONSE_DECODE_ERROR);
        }

        if (fsdpResponseEntity.getCode() != 1 && fsdpResponseEntity.getO_CODE() != 1) {
            log.error(String.format("请求%s失败,FSDP失败原因:%s",param.getString("serviceId"),fsdpResponseEntity.getO_NOTE()));
            throw new GlobalException(ServiceError.REQUEST_RESPONSE_ERROR);
        }
        log.info(String.format("请求%s成功,耗时:%s",param.getString("serviceId"),fsdpResponseEntity.getDuration()));
        return StringUtils.isEmpty(fsdpResponseEntity.getO_RESULT())?fsdpResponseEntity.getRecords():fsdpResponseEntity.getO_RESULT();
    }

    /**
     * 统一请求fsdp方法
     * @param param  请求入参
     * @return  fsdp响应
     */
    public FsdpResponseEntity requestFsdpFullData(JSONObject param, String serviceId) {
        param.put("serviceId", serviceId);
        String fsdpParam = param.toJSONString();
        log.info("FSDP请求参数：{}", fsdpParam);
        ResponseEntity<String>  stringResponseEntity;
        try {
            stringResponseEntity = fsdpService.getFsdpInfo(fsdpParam);
        } catch (Exception ex) {
            log.error("请求fsdp异常,e={}", ex);
            throw new GlobalException(ServiceError.REQUEST_FSDP_ERROR);
        }
        if(stringResponseEntity.getStatusCode()!= HttpStatus.OK||StringUtils.isEmpty(stringResponseEntity.getBody()))
            throw new GlobalException(ServiceError.REQUEST_RESPONSE_ERROR);
        FsdpResponseEntity fsdpResponseEntity=null;
        try {
             fsdpResponseEntity= JSON.parseObject(URLDecoder.decode(stringResponseEntity.getBody(),"UTF-8"), FsdpResponseEntity.class);
            log.info(String.format("响应%s为%s",param.getString("serviceId"),JSON.toJSONString(fsdpResponseEntity)));
            if(fsdpResponseEntity==null){
                log.error("解析FSDP响应体为null");
                throw new GlobalException(ServiceError.REQUEST_RESPONSE_ERROR);
            }
        } catch (UnsupportedEncodingException e) {
            log.error(String.format("解密FSDP或者转换类型错误,response=%s,e=%s",stringResponseEntity.getBody(),e));
            throw new GlobalException(ServiceError.RESPONSE_DECODE_ERROR);
        }

        if (fsdpResponseEntity.getCode() != 1 && fsdpResponseEntity.getO_CODE() != 1) {
            log.error(String.format("请求%s失败,FSDP失败原因:%s",param.getString("serviceId"),fsdpResponseEntity.getO_NOTE()));
            throw new GlobalException(ServiceError.REQUEST_RESPONSE_ERROR);
        }
        log.info(String.format("请求%s成功,耗时:%s",param.getString("serviceId"),fsdpResponseEntity.getDuration()));
        return fsdpResponseEntity ;
    }

    /**
     * 统一请求fsdp方法
     * @param param  请求入参
     * @return  fsdp响应
     */
    public  FsdpResponseEntity requestFsdp2(JSONObject param, String serviceId) {
        param.put("serviceId", serviceId);
        String fsdpParam = param.toJSONString();
        log.info("FSDP请求参数：{}", fsdpParam);
        ResponseEntity<String>  stringResponseEntity;
        try {
            stringResponseEntity = fsdpService.getFsdpInfo(fsdpParam);
        } catch (Exception ex) {
            log.error("请求fsdp异常,e={}", ex);
            throw new GlobalException(ServiceError.REQUEST_FSDP_ERROR);
        }
        if(stringResponseEntity.getStatusCode()!= HttpStatus.OK||StringUtils.isEmpty(stringResponseEntity.getBody()))
            throw new GlobalException(ServiceError.REQUEST_RESPONSE_ERROR);
        FsdpResponseEntity fsdpResponseEntity=null;
        try {
            fsdpResponseEntity= JSON.parseObject(URLDecoder.decode(stringResponseEntity.getBody(),"UTF-8"), FsdpResponseEntity.class);
            log.info(String.format("响应%s为%s",param.getString("serviceId"),JSON.toJSONString(fsdpResponseEntity)));
            if(fsdpResponseEntity==null){
                log.error("解析FSDP响应体为null");
                throw new GlobalException(ServiceError.REQUEST_RESPONSE_ERROR);
            }
        } catch (UnsupportedEncodingException e) {
            log.error(String.format("解密FSDP或者转换类型错误,response=%s,e=%s",stringResponseEntity.getBody(),e));
            throw new GlobalException(ServiceError.RESPONSE_DECODE_ERROR);
        }

        if (fsdpResponseEntity.getCode() != 1 && fsdpResponseEntity.getO_CODE() != 1) {
            log.error(String.format("请求%s失败,FSDP失败原因:%s",param.getString("serviceId"),fsdpResponseEntity.getO_NOTE()));
            throw new GlobalException(ServiceError.REQUEST_RESPONSE_ERROR);
        }
        log.info(String.format("请求%s成功,耗时:%s",param.getString("serviceId"),fsdpResponseEntity.getDuration()));
        return fsdpResponseEntity;
    }
}
